package RoleList

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

const (
	err_ok      = 1
	err_timeout = 2
)

type transactionRoleList struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.RoleListToC
	errcode int
}

func (this *transactionRoleList) GetModuleName() string {
	return "RoleList"
}

func (this *transactionRoleList) GetReplyMsg() proto.Message {
	return &cs_message.RoleListToC{}
}

func (this *transactionRoleList) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionRoleList) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionRoleList) Begin() {
	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	this.resp = roleModule.PackRoleList()
	this.errcode = err_ok
	this.user.EndTrans(this)
}

func (this *transactionRoleList) End() {
	if this.errcode == err_ok {
		this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_OK).Enum()
	} else {
		this.resp = &cs_message.RoleListToC{
			Errcode: cs_message.EnumType(cs_message.EnumType_RETRY).Enum(),
		}
	}
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionRoleList) Timeout() {
	this.errcode = err_timeout
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.RoleListToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionRoleList{
			user: user,
			req:  msg,
		}
	})
}
